package com.onefinger.manager.views.model
{
	import com.onefinger.manager.events.ManagerEvent;
	import com.onefinger.manager.vos.Category;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	public class CategoryCRUDModel extends BaseCRUDModel
	{
		
		
		//--------------------------------------------------------------------
		//    Public Setters and Getters
		//--------------------------------------------------------------------
		
		//.................categories......................
		private var _categories:ArrayCollection;
		
		[Bindable(event="changeCategories")]	
		public function get categories():ArrayCollection
		{
			return _categories;
		}
		public function set categories(value:ArrayCollection):void
		{
			_categories = value;
			dispatchEvent( new Event( "changeCategories" ) );
		}
		
		//.................selectedCategory......................
		private var _selectedCategory:Category;
		
		[Bindable(event="changeCategory")]
		public function get selectedCategory():Category
		{
			return _selectedCategory;
		}
		
		public function set selectedCategory(value:Category):void
		{
			_selectedCategory = value;
			dispatchEvent( new Event( "changeCategory" ) );
		}
		
		//.................tempCategory......................
		[Bindable(event="changeTempCategory")]		
		public function get tempCategory():Category
		{
			return _tempCategory;
		}

		public function set tempCategory(value:Category):void
		{
			_tempCategory = value;
			dispatchEvent(new Event("changeTempCategory"));
		}

		//--------------------------------------------------------------------
		//    Constructor
		//--------------------------------------------------------------------
		public function CategoryCRUDModel(dispatcher:IEventDispatcher)
		{
			super(dispatcher);
		}
		//--------------------------------------------------------------------
		//    Public Methods
		//--------------------------------------------------------------------
		
		//.................selectCategory......................
		override public function select(category:Object):void
		{
			super.select(category);
			tempCategory= category.clone();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_CATEGORY );
			event.category = category as Category;
			dispatcher.dispatchEvent( event );
		}
		override public function edit(category:Object):void
		{
			super.edit(category);
			select(category);
			stateIndex = BaseCRUDModel.EDITING;
		}
		//.................deleteCategory......................
		override public function remove():void
		{
			super.remove();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.DELETE_CATEGORY );
			event.category = selectedCategory;
			dispatcher.dispatchEvent( event );
			unselect();
		}
		
		//.................addNewCategory......................
		override public function addNew():void
		{
			super.addNew();
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.SAVE_CATEGORY);
			evet.category = tempCategory;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateCategory......................
		override public function update():void
		{
			super.update();
			selectedCategory.name=tempCategory.name;
			selectedCategory.icon=tempCategory.icon;
			selectedCategory.showOrder = tempCategory.showOrder;
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.UPDATE_CATEGORY);
			evet.category = selectedCategory;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateCategory......................
		override public function unselect():void
		{
			super.unselect();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_CATEGORY );
			event.category = new Category();
			tempCategory = new Category();
			dispatcher.dispatchEvent( event );
		}
		//.................cancel......................
		override public function cancel():void
		{
			super.cancel();
			selectedCategory=new Category();
			tempCategory = new Category();
			stateIndex=BaseCRUDModel.CREATING;
			
		}
		
		//--------------------------------------------------------------------
		//   update on change
		//--------------------------------------------------------------------
		private var _tempCategory:Category=new Category();
		
		public function updateSelectedIcon(value:String):void
		{
			tempCategory.icon=value;
		}
		public function updateCategoryName(value:String):void
		{
			tempCategory.name=value;	
		}
		public function updateShowOrder(value:int):void{
			tempCategory.showOrder=value;
		}


	}
}